using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace eTMS.Controls
{
    public class CheckBox : System.Windows.Forms.CheckBox
    {
        public CheckBox()
            : base()
        {
            this.SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, true);
            base.BackColor = Color.Transparent;
        }

        public event EventHandler OnPressEnter;
        public event EventHandler OnPressLeft;
        public event EventHandler OnPressRight;
        public event EventHandler OnPressUp;
        public event EventHandler OnPressDown;
        public event EventHandler OnPressPageDown;
        public event EventHandler OnPressPageUp;
        private Control _PrevText;
        private Control _NextText;
        public Control PrevText
        {
            set
            {
                _PrevText = value;
            }
            get
            {
                return _PrevText;

            }
        }
        public Control NextText
        {
            set
            {
                _NextText = value;
            }
            get
            {
                return _NextText;

            }
        }
        private void SendTab()
        {
            SendKeys.Send("{TAB}");
        }
        private void SendShiftTab()
        {
            SendKeys.Send("{SHIFT}{TAB}");
        }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (OnPressEnter != null) OnPressEnter(this, e);
                    if (_NextText != null)
                    {
                        if (_NextText is Button)
                        {
                            ((Button)_NextText).PerformClick();
                        }
                        else
                        {
                            _NextText.Focus();
                        }
                    }
                    else
                    {
                        SendTab();
                    }
                    e.Handled = true;
                    break;
                case Keys.Left:
                    if (OnPressLeft != null) OnPressLeft(this, e);
                    if (_PrevText != null)
                    {
                        if (_PrevText is Button)
                        {
                            ((Button)_PrevText).PerformClick();
                        }
                        else
                        {
                            _PrevText.Focus();
                        }
                    }
                    else
                    {
                        SendShiftTab();
                    }
                    break;
                case Keys.Right:
                    if (OnPressRight != null) OnPressRight(this, e);
                    if (_NextText != null)
                    {
                        if (_NextText is Button)
                        {
                            ((Button)_NextText).PerformClick();
                        }
                        else
                        {
                            _NextText.Focus();
                        }
                    }
                    else
                    {
                        SendTab();
                    }
                    break;
                case Keys.Up:
                    if (OnPressUp != null) OnPressUp(this, e);
                    break;
                case Keys.Down:
                    if (OnPressDown != null) OnPressDown(this, e);
                    break;
                case Keys.PageDown:
                    if (OnPressPageDown != null) OnPressPageDown(this, e);
                    break;
                case Keys.PageUp:
                    if (OnPressPageUp != null) OnPressPageUp(this, e);
                    break;
            }
        }
    }

}
